package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.model.JSONResult;
import com.example.demo.model.json.VideoApp;
import com.example.demo.model.json.VideoClassfyData;
import com.example.demo.model.json.VideoData;
import com.example.demo.respository.VideoAppRepository;
import com.example.demo.respository.VideoClassfyRepository;
import com.example.demo.respository.VideoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/video_action")
public class VideoController {

    @Autowired
    private VideoRepository videoRepository;
    @Autowired
    private VideoClassfyRepository videoClassfyRepository;

    @Autowired
    private VideoAppRepository videoAppRepository;

    /**
     * 该接口使用
     * 1.应用自升级
     * <p>
     * http://localhost:8081/video_action/videoUpdate
     */
    @GetMapping("/videoUpdate")
    public JSONResult videoUpdate(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        VideoApp versionData = new VideoApp();


        Iterable<VideoApp> video_list = videoAppRepository.findAll();
        Iterator iterator = video_list.iterator();

        while (iterator.hasNext()) {
            versionData = (VideoApp) iterator.next();
        }

        jsonResult.putJSONData(0, "get videocAdvertising success", "video_action/videocAdvertising", versionData);

        return jsonResult;
    }

    /**
     * 该接口使用
     * 2.广告信息
     * <p>
     * http://localhost:8081/video_action/videocAdvertising
     */
    @GetMapping("/videocAdvertising")
    public JSONResult videocAdvertising(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        VideoApp versionData = new VideoApp();


        Iterable<VideoApp> video_list = videoAppRepository.findAll();
        Iterator iterator = video_list.iterator();

        while (iterator.hasNext()) {
            versionData = (VideoApp) iterator.next();
        }
        jsonResult.putJSONData(0, "get videocAdvertising success", "video_action/videocAdvertising", versionData);

        return jsonResult;
    }

    /**
     * http://localhost:8090/video_action/videoclean
     */
    @GetMapping("/videoclean")
    public JSONResult videocleanlist(HttpServletRequest request) {

        String basePath = request.getServletContext().getRealPath("/");
        System.err.println("basePath====>>" + basePath);

        Iterable<VideoData> video_list = videoRepository.findAll();
        Iterator iterator = video_list.iterator();

        JSONResult jsonResult = new JSONResult();
        while (iterator.hasNext()) {
            VideoData appData = (VideoData) iterator.next();

            appData.setVideo_url(appData.getVideo_url().replace("https://fun4demo-1254407438.cos.ap-guangzhou.myqcloud.com", ""));
            appData.setThumbnail(appData.getThumbnail().replace("https://fun4demo-1254407438.cos.ap-guangzhou.myqcloud.com", ""));
            videoRepository.save(appData);
        }

        return jsonResult;
    }

    /**
     * 接收回调
     */
    @PostMapping("/callback")
    public String uploadCallback(HttpServletRequest request) {
        return "";
    }


    /**
     * 视频删除  success
     * 1.   删除视频资源
     * 2.   删除略缩图
     * 3.   删除数据内容
     * http://localhost:8090/video_action/videodelete
     */
    @PostMapping("/videodelete")
    public JSONResult deleteVideo(HttpServletRequest request) {
        String id = request.getParameter("id");
        String video_url = request.getParameter("video_url");
        String image_url = request.getParameter("image_url");
        String thumbnail = request.getParameter("thumbnail");

        if (video_url != null) {

        }

        if (image_url != null) {

        }

        if (thumbnail != null) {

        }

        videoRepository.deleteById(Long.parseLong(id));

        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "delete video success",
                "video_action/videodelete", "");

        return jsonResult;
    }

    /**
     * 视频详情  success
     * http://localhost:8090/video_action/videodetail
     */
    @GetMapping("/videodetail")
    public JSONResult videoDetail(HttpServletRequest request) {
        String id = request.getParameter("id");
        Optional<VideoData> bbc = videoRepository.findById(Long.parseLong(id));
        VideoData videoData = bbc.get();

        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "query video detail success",
                "video_action/videodetail", JSON.toJSONString(videoData));

        return jsonResult;
    }

    /**
     * 保存app
     * http://localhost:8090/video_action/savevideo
     */
    @PostMapping("/savevideo")
    public JSONResult saveVideo(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String show = request.getParameter("show");
        System.err.println("saveVideo====>>" + id + "    show:" + show);
        String content = request.getParameter("content");
        String classfyId = request.getParameter("classfyId");
        String author = request.getParameter("author");
        String video_url = request.getParameter("video_url");
        String image_url = request.getParameter("image_url");
        String thumbnail = request.getParameter("thumbnail");
        String width = request.getParameter("width");
        String height = request.getParameter("height");
        String rotation = request.getParameter("rotation");
        String duration = request.getParameter("duration");
        String top = request.getParameter("top");

        VideoData videoData;
        if (id == null || id.isEmpty()) {
            videoData = new VideoData();
            System.err.println("saveVideo====>>hello01");
        } else {
            Optional<VideoData> bbc = videoRepository.findById(Long.parseLong(id));
            videoData = bbc.get();
            System.err.println("saveVideo====>>hello02:" + videoData.getId());
        }

        videoData.setName(name);
        videoData.setContent(content);
        videoData.setAuthor(author);

        if (classfyId != null) {
            videoData.setClassfyId(Long.parseLong(classfyId));

        } else {
            videoData.setClassfyId(-1l);
        }

        if (video_url != null) {
            videoData.setVideo_url(video_url);
        }

        if (image_url != null) {
            videoData.setImage_url(image_url);
        }

        if (thumbnail != null) {
            videoData.setThumbnail(thumbnail);
        }

        if (width != null) {
            videoData.setWidth(width);
        }
        if (height != null) {
            videoData.setHeight(height);
        }
        if (rotation != null) {
            videoData.setRotation(rotation);
        }
        if (duration != null) {
            videoData.setDuration(duration);
        }

        if (top != null) {
            videoData.setTop(top);
        }

        videoRepository.save(videoData);
        JSONResult jsonResult = new JSONResult();
        jsonResult.putJSONData(200, "save video success", "video_action/savevideo", "保存成功");

        return jsonResult;
    }

    
    /**
     * 查词所有  http://localhost:8090/video_action/video_all_list
     */
    @GetMapping("/video_all_list")
    public JSONResult getVideoAlllist(HttpServletRequest request) {
        System.err.println("getVideoAlllist====>>:hello" );

        Iterable<VideoData> video_list;
        video_list = videoRepository.findAll();

        List<VideoData> video_listbb = new ArrayList<>();
        Iterator iterator = video_list.iterator();

        JSONResult jsonResult = new JSONResult();
        while (iterator.hasNext()) {
            VideoData appData = (VideoData) iterator.next();
            video_listbb.add(appData);
        }


        jsonResult.putJSONData(0, "get list success", "video_action/video_all_list", video_listbb);
        System.err.println("videolist_nocalss====>>" + jsonResult.toJSONString());
        return jsonResult;
    }
    
    /**
     * 查词所有  http://localhost:8090/video_action/videolist
     */
    @GetMapping("/videolist")
    public JSONResult getVideolist(HttpServletRequest request) {
        String classfyId = request.getParameter("classfyId");
        System.err.println("videolist====>>classfyId:" + classfyId);

        Iterable<VideoData> video_list;
        if (classfyId != null) {
            video_list = videoRepository.findByclassfyId(Long.parseLong(classfyId));

        } else {
            video_list = videoRepository.findAll();
        }

        List<VideoData> video_listbb = new ArrayList<>();
        Iterator iterator = video_list.iterator();

        JSONResult jsonResult = new JSONResult();
        while (iterator.hasNext()) {
            VideoData appData = (VideoData) iterator.next();
            video_listbb.add(appData);
        }


        jsonResult.putJSONData(0, "get list success", "video_action/videolist", video_listbb);
        System.err.println("video_listbb====>>" + jsonResult.toJSONString());
        return jsonResult;
    }


    /**
     * 查词所有分类  http://localhost:8090/video_action/classlist
     */
    @GetMapping("/classlist")
    public JSONResult getClasslist(HttpServletRequest request) {
        System.err.println("getClasslist====>>");
        Iterable<VideoClassfyData> class_list = videoClassfyRepository.findAll();

        List<VideoClassfyData> class_listbb = new ArrayList<>();
        Iterator iterator = class_list.iterator();

        JSONResult jsonResult = new JSONResult();
        while (iterator.hasNext()) {
            VideoClassfyData appData = (VideoClassfyData) iterator.next();
            class_listbb.add(appData);
        }

        jsonResult.putJSONData(0, "get class list success", "video_action/classlist", class_listbb);
        System.err.println("class====>>" + jsonResult.toJSONString());
        return jsonResult;
    }
}